package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.enums.NovelConst;
import com.ry.novel.enums.ResponseStatus;
import com.ry.novel.exception.BusinessException;
import com.ry.novel.mapper.SysUserMapper;
import com.ry.novel.model.dto.SysUserInfo;
import com.ry.novel.model.dto.UserInfo;
import com.ry.novel.model.dto.condition.SysUserQuery;
import com.ry.novel.model.entity.SysUser;
import com.ry.novel.model.entity.User;
import com.ry.novel.service.SysUserService;
import com.ry.novel.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-05-01 19:59
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUserInfo login(SysUserInfo sysUserInfo) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",sysUserInfo.getUsername());
        queryWrapper.eq("password", MD5Util.MD5Encode(sysUserInfo.getPassword()));
        SysUser entity = sysUserMapper.selectOne(queryWrapper);
        if (null == entity){
            //没有对应的用户
            throw new BusinessException(ResponseStatus.USERNAME_PASSWORD_ERROR);
        }

        SysUserInfo info = new SysUserInfo();
        info.setId(entity.getId());
        info.setUsername(entity.getUsername());

        return info;
    }

    @Override
    public Page<SysUserInfo> querySysUser(SysUserQuery sysUserQuery) {
        Page<SysUser> page = new Page<>(sysUserQuery.getPage(), sysUserQuery.getSize());
        Page<SysUserInfo> infoPage = new Page<>(sysUserQuery.getPage(), sysUserQuery.getSize());
        QueryWrapper<SysUser> wrapper = new QueryWrapper();
        if (sysUserQuery.getUsername() != null && sysUserQuery.getUsername().length() != 0) {
            wrapper.eq("username", sysUserQuery.getUsername());
        }

        sysUserMapper.selectPage(page, wrapper);

        List<SysUserInfo> infos = page.getRecords().stream().map(entity -> {
            SysUserInfo info = new SysUserInfo();
            info.setId(entity.getId());
            info.setUsername(entity.getUsername());
            info.setPassword(entity.getPassword());

            return info;
        }).collect(Collectors.toList());

        infoPage.setRecords(infos);
        infoPage.setTotal(page.getTotal());

        return infoPage;
    }

    @Override
    public void add(SysUserInfo sysUserInfo) {
        SysUser entity = new SysUser();
        entity.setUsername(sysUserInfo.getUsername());
        String encodePW = MD5Util.MD5Encode(sysUserInfo.getPassword(), Charset.forName("utf8").name());
        entity.setPassword(encodePW);

        sysUserMapper.insert(entity);
    }

    @Override
    public void update(SysUserInfo sysUserInfo) {
        SysUser entity = new SysUser();
        if (sysUserInfo.getId() == 1){
            throw new BusinessException(ResponseStatus.SYS_USER_UPDATE_ERROR);
        }
        entity.setId(sysUserInfo.getId());
        entity.setUsername(sysUserInfo.getUsername());
        entity.setPassword(sysUserInfo.getPassword());

        sysUserMapper.updateById(entity);
    }

    @Override
    public void deleteById(Long id) {
        if (id == 1){
            throw new BusinessException(ResponseStatus.SYS_USER_DELETE_ERROR);
        }
        sysUserMapper.deleteById(id);
    }
}
